#ifndef LIBACCENT_ALGORITHMS_REVERSE_HPP
#define LIBACCENT_ALGORITHMS_REVERSE_HPP

#include <utility>

namespace accent
{

  template <typename BidirectionalRange>
  void reverse(BidirectionalRange r)
  {
    while (!r.empty()) {
      auto s = r;
      s.pop_front();
      if (!s.empty()) {
        using std::swap;
        swap(r.front(), r.back());
        s.pop_back();
      }
      r = s;
    }
  }

  template <typename BidirectionalRange>
  typename BidirectionalRange::position
    reverse_with_position(BidirectionalRange r,
                          typename BidirectionalRange::position p)
  {
    typename BidirectionalRange::position result = p;
    while (!r.empty()) {
      auto s = r;
      s.pop_front();
      if (!s.empty()) {
        using std::swap;
        swap(r.front(), r.back());
        s.pop_back();
        // check if this is the mirror position for the location
        if (p == r.front_position())
          result = r.back_position();
        else if (p == r.back_position())
          result = r.front_position();
      }
      r = s;
    }
    return result;
  }

}

#endif
